const Koa = require("Koa");
const Router = require("koa-router");
const Session = require("koa-session");

const app = new Koa();

// 创建Session配置
const session = Session(
  {
    key: "sessionid",
    maxAge: 10 * 1000,
    // 是否使用加密签名
    signed: true,
  },
  app
);
app.keys = ["aaa"];

app.use(session);

const testRouter = new Router();
testRouter.get("/test", (ctx, next) => {
  const id = 110;
  const name = "coderwhy";

  ctx.session.user = { id, name };

  ctx.body = "test";
});
testRouter.get("/demo", (ctx, next) => {
  console.log(ctx.session.user);
  ctx.body = "test";
});

app.use(testRouter.routes());
app.use(testRouter.allowedMethods());

app.listen(8080, () => {
  console.log("服务器启动成功");
});
